//
//  ScopeConvertDetailViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/26.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopeConvertDetailViewController.h"
#import "LineTransferInputTableViewCell.h"
#import "LineTransferTextCell.h"
#import "ReplaceDriveDetailCallCell.h"
#import "LineTransferTableViewCell.h"

static NSString *lineCell = @"lineTransfer";
static NSString *lineInput = @"lineInputTransfer";
static NSString *lineTextCell = @"lineTransferText";
static NSString *detailCall = @"detailcall";

@interface ScopeConvertDetailViewController (){
    LineTransferTextCell *textCell;
}

@end

@implementation ScopeConvertDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self setUpViewControllerDataSource];
}



- (void)setUpViewControllerDataSource {
    if(_type == 0){ //油卡兑换
        [self.tableView registerNib:[UINib nibWithNibName:@"LineTransferInputTableViewCell" bundle:nil] forCellReuseIdentifier:lineInput];
    }else { //服务兑换
         [self.tableView registerNib:[UINib nibWithNibName:@"ReplaceDriveDetailCallCell" bundle:nil] forCellReuseIdentifier:detailCall];
    }
    
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    _carAround.shopId = _goodId;
    _carAround.converType = [NSString stringWithFormat:@"%d",_type];
    [_carAround scopeEchangeDetail:^(id result) {
        _info = [result customObjectForkey: _type == 0 ? @"card_obj" : @"service_obj"];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
}

- (void)initDataSource {
    //POINTS_REQUIRED
   
    if (!_info || !_info.allValues.count > 0) {
        return;
    }
    _currenScope.text = [NSString stringWithFormat:@"%@",[_info customObjectForkey:@"mvb"]];
    _userScope.text = [_info customObjectForkey:@"POINTS_REQUIRED"];
    if(_type == 1){
        _tableDataSource = [NSMutableArray  arrayWithArray:[[_info customObjectForkey:@"TEL"] componentsSeparatedByString:@","]];
        int i = 0 ;
        BOOL replace = NO;
        for (int a = 0 ;a < _tableDataSource.count; a ++) {
            NSString *str = [_tableDataSource objectAtIndex:a];
            if ([str isEqualToString:@"null"]) {
                i = a;
                replace = YES;
            }
        }
        if (replace) {
            [_tableDataSource removeObjectAtIndex:i];
        }
    }
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}

- (void)initAbout {
    self.title = @"积分兑换";
    self.tableView.tableFooterView = [UIView new];
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"LineTransferTableViewCell" bundle:nil] forCellReuseIdentifier:lineCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LineTransferTextCell" bundle:nil] forCellReuseIdentifier:lineTextCell];
}

#pragma mark - tableView delegate & dataSource 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:{
            return 1;
            break;
        }
        case 1:
        {
            if (_type == 0) { //油卡兑换
                return 1;
            }else { //服务兑换
                return _tableDataSource.count <= 0 ? 0 : _tableDataSource.count;
            }
            break;
        }
        case 2:{
            return 1;
            break;
     
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case  0:
        {
            LineTransferTableViewCell *line = [tableView dequeueReusableCellWithIdentifier:lineCell forIndexPath:indexPath];
            line.info = _info;
            return line;
            break;
        }
        case 1:{
            if (_type == 0) { //油卡兑换
                LineTransferInputTableViewCell *input = [tableView dequeueReusableCellWithIdentifier:lineInput forIndexPath:indexPath];
                input.target = self;
                input.action = @selector(inputSendAction:);
                return input;
            }else {//服务兑换
                ReplaceDriveDetailCallCell *drive = [tableView dequeueReusableCellWithIdentifier:detailCall forIndexPath:indexPath];
                drive.count = indexPath.row;
                drive.phone.text = [_tableDataSource objectAtIndex:indexPath.row];
                return drive;
            }
            
            break;
        }
        case 2:{
            textCell = [tableView dequeueReusableCellWithIdentifier:lineTextCell forIndexPath:indexPath];
            textCell.detail = [_info customObjectForkey:@"PRODUCT_DESC"];
            NSLog(@"detail:%@",textCell.detail);
            return textCell;
            break;
        }
        default:
            return nil;
            break;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case  0:
        {
            return 90;
            break;
        }
        case 1:{
            if (_type == 0) { //油卡兑换
                return 88;
            }else {//服务兑换
                return 44;
            }
            
            break;
        }
        case 2:{
            return textCell.height;
            break;
        }
        default:
            return 0;
            break;
    }
    return 0;
}

- (void)inputSendAction:(NSDictionary *)dic {
    _name = [dic customObjectForkey:@"name"];
    _oilCard = [dic customObjectForkey:@"oilCard"];
    if (_name.length ==0) {
        [self.view makeToast:@"请输入姓名!"];
        return;
    }
    if (_oilCard.length == 0) {
        [self.view makeToast:@"请输入油卡号!"];
        return;
    }
   
    _carAround.oilCard = _oilCard;
    _carAround.name = _name;
    _carAround.vb = _userScope.text;
    [_carAround scopeEchangeOilCard:^(id result) {
        [self.navigationController.view makeToast:@"兑换成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } anfFail:^(id result) {
        
    }];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


//提交:
- (IBAction)submitAction:(id)sender {
    if (_type == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getOilCard" object:nil];
    }else {
      
        _carAround.shopId = [_info customObjectForkey:@"SELLER_ID"];
        _carAround.goodId = [_info customObjectForkey:@"ID"];
        _carAround.name = [_info customObjectForkey:@"PRODUCT_NAME"];
        _carAround.vb = _userScope.text;
        [_carAround scopeEchangeService:^(id result) {
            [self.navigationController.view makeToast:@"兑换成功"];
            [self.navigationController popViewControllerAnimated:YES];
            
        } anfFail:^(id result) {
            
        }];
    }
  
}
@end
